package cn.jlonely.xyhx.enableScheduling;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class RankingStatusUpdater {

    private final JdbcTemplate jdbcTemplate;

    public RankingStatusUpdater(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 每分钟更新排行榜状态
     */
    @Scheduled(cron = "0 * * * * ?")
    public void updateRankingStatus() {
        String sql = """
                    UPDATE xyhx.ranking_table 
                    SET ranking_status = CASE 
                        WHEN NOW() < start_time THEN 0 
                        WHEN NOW() BETWEEN start_time AND end_time THEN 1 
                        ELSE 2 
                    END
                    WHERE ranking_status != CASE 
                        WHEN NOW() < start_time THEN 0 
                        WHEN NOW() BETWEEN start_time AND end_time THEN 1 
                        ELSE 2 
                    END
                """;

        jdbcTemplate.update(sql);

    }
}
